/**
 * 填写快递信息
 * Created by ZeonWang on 2017/3/31.
 */
$(function(){

    var $express_input = $('#express');
    var $express_no_input = $('#express_no');
    var $express_confirm_btn = $('#express_confirm');
    var token = $('#editor-form-bu')[0]['_token'].value;
    var $back_btn = $('#express_back');

    $('.dataTable').delegate('.express-editor-btn','click',function () {

        var update_href = $(this)[0].dataset.href;

        //初始化快递选择下拉框
        for ( var key in EXPRESS_CODE_MAP ){

            $express_input.append("<option value='"+ key +"'>"+ EXPRESS_CODE_MAP[key] +"</option>")

        }

        $express_input.select2();

        // reset input
        $express_input.val('').trigger('change');
        $express_no_input.val('');

        $.ajax({
            method: "GET",
            url: update_href
        }).done(function(data){
            $express_input.val(data.express_company_code).trigger('change');
            $express_no_input.val(data.expressage_code);

            // inject href to confirm btn
            $express_confirm_btn.data('href',update_href);
        })

    });

    $express_confirm_btn.on('click',function( evt ){

        if (  ! $express_input.val() ||
            ! $express_no_input.val()
        ) {
            return alert('不能为空');
        }

        $.ajax({
            method: "POST",
            url: $(this).data('href') ,
            data : {
                '_method' : 'PUT',
                '_token' : token,
                'express' : $express_input.val(),
                'express_no' : $express_no_input.val()
            }
        }).done(function(data){

            alert('成功修改');
            $back_btn.trigger('click');
        });

    });


});